﻿using MCMesServer.DBFunctions.TransformFuncs;
using MesBLL.EventService;
using MesDAL.DBInfo;
using MesDAL.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MCMesServer.DBFunctions.DBFuncs
{
    public class MachineInfoDBFunc
    {
        /// <summary>
        /// 查询设备的状态
        /// </summary>
        /// <returns></returns>
        public async static Task<List<MachineInfoDTO>> SelectFormMachineStatus()
        {
            using (var machineSer = new MachineInfoService())
            {
                List<machine_info> machine_list = await machineSer.SelectFormMachineStatus();
                List<MachineInfoDTO> machineDTO_list = new List<MachineInfoDTO>();

                for (int i = 0; i < machine_list.Count; i++)
                {
                    machineDTO_list.Add(TransformDTOFunc.TransformModel2MachineInfoDTO(machine_list[i]));
                }
                return machineDTO_list;
            }
        }

        /// <summary>
        /// 通过设备号检索设备信息
        /// </summary>
        /// <param name="machineNum"></param>
        /// <returns></returns>
        public async static Task<MachineInfoDTO> SelectMachineInfoByMachineNum(string machineNum)
        {
            using (var machineSer = new MachineInfoService())
            {
                machine_info machine_list = await machineSer.SelectMachineInfoByMachineNum(machineNum);

                MachineInfoDTO machineDTO = TransformDTOFunc.TransformModel2MachineInfoDTO(machine_list);
                return machineDTO;
            }
        }

        /// <summary>
        /// 编辑设备信息
        /// </summary>
        /// <param name="machineInfo"></param>
        /// <returns></returns>
        public async static Task<MachineInfoDTO> EditMachineInfo(MachineInfoDTO machineInfo)
        {
            using (var machineSer = new MachineInfoService())
            {
                machine_info needEdit = TransformDTOFunc.TransformMachineInfo2Model(machineInfo);
                machine_info machineEdit = await machineSer.EditAsync(needEdit);
                MachineInfoDTO machineDTO = TransformDTOFunc.TransformModel2MachineInfoDTO(machineEdit);
                return machineDTO;
            }
        }

        public async static Task<List<MachineInfoDTO>> SelectMachineInfoByMachineClass(string name)
        {
            using (var machineSer = new MachineInfoService())
            {
                List<machine_info> machinelist = await machineSer.SelectMachineInfoByMachineClass(name);
                List<MachineInfoDTO> list = new List<MachineInfoDTO>();
                for (int i = 0; i < machinelist.Count; i++)
                {
                    list.Add(TransformDTOFunc.TransformModel2MachineInfoDTO(machinelist[i]));
                }
                return list;
            }
        }

        public async static Task<List<MachineInfoDTO>> SelectMachineInfoMessage()
        {
            using (var machineSer = new MachineInfoService())
            {
                List<machine_info> machine_list = await machineSer.SelectMachineInfoMessage();
                List<MachineInfoDTO> list = new List<MachineInfoDTO>();
                for (int i = 0; i < machine_list.Count; i++)
                {
                    list.Add(TransformDTOFunc.TransformModel2MachineInfoDTO(machine_list[i]));
                }
                return list;
            }
        }
    }
}
